/**
 * Copyright 2013 ABSir's Studio
 * 
 * All right reserved
 *
 * Create on 2013-8-27 下午1:01:44
 */
package com.absir.android.batch.store;

import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;

import android.app.Application;
import cn.waps.AppConnect;

import com.absir.android.goods.CoinGoodsDefine;
import com.absir.android.goods.GoodsInfo;
import com.absir.android.goods.IGoodsInfo;
import com.absir.bean.inject.value.Bean;
import com.absir.bean.inject.value.Inject;
import com.absir.bean.inject.value.InjectType;

/**
 * @author absir
 * 
 */
@Bean
public class SimpleGoodsDefine extends CoinGoodsDefine {

	/** goodsInfoCaches */
	private Map<Object, IGoodsInfo> goodsInfoCaches;

	/**
	 * @param application
	 */
	@Inject(type = InjectType.Selectable)
	protected void setApplication(Application application) {
		// Waps广告平台
		AppConnect.getInstance("50d1dfd15836d6b190f1df6d484ed149", "Waps", application);
		// try {
		// ApplicationInfo applicationInfo =
		// application.getPackageManager().getApplicationInfo(application.getPackageName(),
		// PackageManager.GET_META_DATA);
		// Bundle metaData = applicationInfo.metaData;
		// if (metaData == null) {
		// metaData = new Bundle();
		// applicationInfo.metaData = metaData;
		// }
		//
		// // System.out.println(metaData.getString("UMENG_APPKEY"));
		// @SuppressWarnings("unchecked")
		// Map<String, Object> mMap = (Map<String, Object>)
		// KernelObject.declaredGet(metaData, "mMap");
		// mMap.put("UMENG_APPKEY", "520657e456240b9b64043192");
		//
		// } catch (Throwable e) {
		// // TODO Auto-generated catch block
		// e.printStackTrace();
		// }
	}

	/**
	 * 
	 */
	@Inject
	protected void afterInject() {
		goodsInfoCaches = new LinkedHashMap<Object, IGoodsInfo>();
		int length = GoodsRepository.Coin_Prices.length - 1;
		for (int i = 0; i < length; i += 2) {
			GoodsInfo goodsInfo = new GoodsInfo();
			goodsInfo.setTag((int) GoodsRepository.Coin_Prices[i]);
			goodsInfo.setPrice(GoodsRepository.Coin_Prices[i + 1]);
			addGoodsInfoCache(goodsInfo);
		}

		for (GoodsRepository goodsRepository : GoodsRepository.values()) {
			GoodsInfo goodsInfo = new GoodsInfo();
			goodsInfo.setTag(goodsRepository.getTag());
			goodsInfo.setPrice(goodsRepository.getPrice());
			addGoodsInfoCache(goodsInfo);
		}
	}

	/**
	 * @param goodsInfo
	 */
	protected void addGoodsInfoCache(GoodsInfo goodsInfo) {
		if (goodsInfo.getPrice() <= 0) {
			return;
		}

		goodsInfo.setGoodsName(getGoodsName(goodsInfo));
		goodsInfo.setGoodsDesc(getGoodsDesc(goodsInfo));
		goodsInfoCaches.put(goodsInfo.getTag(), goodsInfo);
	}

	/**
	 * @param goodsInfo
	 * @return
	 */
	protected String getGoodsName(IGoodsInfo goodsInfo) {
		if (goodsInfo.getGoodsName() == null) {
			if (goodsInfo.getTag() instanceof Integer) {
				return goodsInfo.getTag() + "金币";

			} else {
				return "道具(" + goodsInfo.getTag() + ")";
			}
		}

		return goodsInfo.getGoodsName();
	}

	/**
	 * @param goodsInfo
	 * @return
	 */
	protected String getGoodsDesc(IGoodsInfo goodsInfo) {
		if (goodsInfo.getGoodsDesc() == null) {
			return goodsInfo.getGoodsName() + " = " + goodsInfo.getPrice() + "$";
		}

		return goodsInfo.getGoodsDesc();
	}

	/**
	 * @param coin
	 * @return
	 */
	protected float getCoinPrice(int coin) {
		return coin * 6f - 0.01f;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.absir.android.goods.IGoodsDefine#getGoodsInfos()
	 */
	@Override
	public synchronized Collection<IGoodsInfo> getGoodsInfos() {
		// TODO Auto-generated method stub
		return goodsInfoCaches.values();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.absir.android.goods.IGoodsDefine#getGoodsInfo(java.lang.Object)
	 */
	@Override
	public IGoodsInfo getGoodsInfo(int coin) {
		// TODO Auto-generated method stub
		IGoodsInfo iGoodsInfo = goodsInfoCaches.get(coin);
		if (iGoodsInfo == null) {
			float price = getCoinPrice(coin);
			if (price > 0) {
				GoodsInfo goodsInfo = new GoodsInfo();
				goodsInfo.setTag(coin);
				goodsInfo.setPrice(price);
				iGoodsInfo = goodsInfo;
				addGoodsInfoCache(goodsInfo);
			}
		}

		return iGoodsInfo;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.absir.android.goods.CoinGoodsDefine#getGoodsInfo(java.lang.String)
	 */
	@Override
	public IGoodsInfo getGoodsInfo(String name) {
		// TODO Auto-generated method stub
		return goodsInfoCaches.get(name);
	}
}
